package two_xia.myPainter;


import two_xia.myPainter.shape.Shape;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Stack;

public class DrawingBoard extends JFrame {

    //用于存储绘画记录
    private ArrayList<Shape> shapeArray = new ArrayList<>();
    private Stack<Shape> undoStack = new Stack<>();

    // 构造函数
    public DrawingBoard() {
        // 设置窗口属性
        super("画板");
        setSize(1000, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        // 初始化界面元素
        initComponents();
    }

    //它需要被获取到,用以演示;不然把属性设置成public属性也可以
    public ArrayList<Shape> getShapeArray() {
        return shapeArray;
    }

    // 初始化界面元素
    private void initComponents() {
        // 用于绘制的区域
        JPanel drawingArea = new DrawingArea(shapeArray, undoStack);
        drawingArea.setBackground(Color.WHITE);
        //实例化事件监听类
        DrawListener drawListener = new DrawListener(drawingArea, shapeArray, undoStack);
        String[] shape = {"椭圆", "矩形", "直线", "铅笔", "三角形", "填充", "橡皮擦", "取色", "撤销", "重做", "打开", "保存"};
        JPanel toolsPanel = new JPanel(new GridLayout(6, 2));
        for (String s : shape) {
            JButton button = new JButton(s);
            button.addActionListener(drawListener);
            toolsPanel.add(button);
        }
        //颜色选取窗口;关于这边，不可以使用构造函数，因为它们相互依赖，只能使用set方法
        CustomColorPanel customColorPanel = new CustomColorPanel(drawListener);
        drawListener.setCustomColorPanel(customColorPanel);
        // 添加组件到主窗口
        add(drawingArea, BorderLayout.CENTER);
        add(toolsPanel, BorderLayout.EAST);
        add(customColorPanel, BorderLayout.SOUTH);

        //将drawingArea作为绘画的区域,接收drawListener中的鼠标监听事件
        drawingArea.addMouseListener(drawListener);
        drawingArea.addMouseMotionListener(drawListener);
        //Graphics gr = this.getGraphics(); 有何作用?
    }

}
